import scrapy
from training_scrapy_01.items import TedItem


class TedSpider(scrapy.Spider):
    name = 'ted'
    # start_urls = ['https://www.ted.com/talks']
    start_urls = ['http://127.0.0.1:8080']

    # //*[@id="browse-results"]/div[1]/div[@class="col"]
    # //*[@id="browse-results"]/div[1]/div[2]
    # //*[@id="browse-results"]/div[1]/div[3]
    # //./div/div/div/div[2]/h4[2]/a
    def parse(self, response):
        results = response.xpath('//*[@id="browse-results"]/div[1]/div[@class="col"]')
        for element in results:
            item = TedItem()
            item['talk'] = element.xpath('./div/div/div/div[2]/h4[2]/a/text()').extract_first()
            item['link'] = element.xpath('./div/div/div/div[2]/h4[2]/a/@href').extract_first()
            yield item


if __name__ == '__main__':
    from scrapy.cmdline import execute
    execute(["scrapy", "crawl", "ted"])